﻿using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Repository;
using System.Linq;
using ZR.Service.IService.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model;
using CKS.Core.Framework;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// 子篮信息Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICksBasketService), ServiceLifetime = LifeTime.Transient)]
    public class CksBasketService : BaseService<CksBasket>, ICksBasketService
    {
        /// <summary>
        /// 查询子篮信息列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CksBasketDto> GetList(CksBasketQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .OrderByDescending(a=>a.UpdateTime)
                .ToPage<CksBasket, CksBasketDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public CksBasket GetInfo(long Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加子篮信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CksBasket AddCksBasket(CksBasket model)
        {
            model.UpdateTime = model.CreatedTime = DateTime.Now;
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改子篮信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCksBasket(CksBasket model)
        {
            model.UpdateTime = DateTime.Now;
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<CksBasket> QueryExp(CksBasketQueryDto parm)
        {
            var exp = Expressionable.Create<CksBasket>();
            exp.AndIF(!parm.BlueCode.IsEmpty(), u => u.BlueCode == parm.BlueCode);
            exp.AndIF(parm.Type != null && parm.Type.Value > -1, u => u.Type == parm.Type.Value);
            return exp;
        }
    }
}